#include "lx_String.h"

void UI_CoordinateWidget::init() {
    lCoord1->setText("");
    lCoord2->setText("");
    lCoord1b->setText("");
    lCoord2b->setText("");
    
    int fsize;
#ifdef __APPLE__
    fsize = 9;
#else
    fsize = 7;
#endif
    
    PF_SETTINGS->beginGroup("/Appearance");
    fsize = PF_SETTINGS->readNumEntry("/StatusBarFontSize", fsize);
    PF_SETTINGS->endGroup();
    
    lCoord1->setFont(QFont("Helvetica", fsize));
    lCoord1b->setFont(QFont("Helvetica", fsize));
    lCoord2->setFont(QFont("Helvetica", fsize));
    lCoord2b->setFont(QFont("Helvetica", fsize));
    
    graphic = NULL;
    prec = 4;
    format = LX_Define::Decimal;
    aprec = 2;
    aformat = LX_Define::DegreesDecimal;
}

void UI_CoordinateWidget::setGraphic(PF_Graphic* graphic) {
    this->graphic = graphic;
    
    setCoordinates(PF_Vector(0.0,0.0), PF_Vector(0.0,0.0), true);
}

/*void QG_CoordinateWidget::setAbsCoordinates(double x, double y) {
    setAbsCoordinates(PF_Vector(x, y));
}

void QG_CoordinateWidget::setAbsCoordinates(const PF_Vector& v) {
    QString str;
    
    str.sprintf("%.4f / %.4f", v.x, v.y);
    lCoord1->setText(str);
    
    double ang = PF_Math::rad2deg(v.angle());
    double rad = v.magnitude();
    str.sprintf("%.4f < %.4f", rad, ang);
    lCoord1b->setText(str);
}

void QG_CoordinateWidget::setAbsCoordinates(const QString& x, const QString& y) {
    lCoord1->setText(x);
    lCoord1b->setText(y);
}

void QG_CoordinateWidget::setRelCoordinates(double x, double y) {
    setRelCoordinates(PF_Vector(x, y));
}

void QG_CoordinateWidget::setRelCoordinates(const PF_Vector& v) {
    QString str;
    str.sprintf("@%.4f / %.4f", v.x, v.y);
    lCoord2->setText(str);
    
    double ang = PF_Math::rad2deg(v.angle());
    double rad = v.magnitude();
    str.sprintf("@%.4f < %.4f", rad, ang);
    lCoord2b->setText(str);
}

void QG_CoordinateWidget::setRelCoordinates(const QString& x, const QString& y) {
    lCoord2->setText(x);
    lCoord2b->setText(y);
}

void QG_CoordinateWidget::setCoordinates(double x, double y,
        double rx, double ry) {
    setAbsCoordinates(x, y);
    setRelCoordinates(rx, ry);
}*/

/*void QG_CoordinateWidget::setAbsCoordinates(const QString& x, 
                                            const QString& y,
                                            const QString& rx,
                                            const QString& ry) {
    lCoord1->setText(x);
    lCoord1b->setText(y);
    lCoord2->setText(rx);
    lCoord2b->setText(ry);
}*/



void UI_CoordinateWidget::setCoordinates(const PF_Vector& abs,
                                         const PF_Vector& rel, bool updateFormat) {
    setCoordinates(abs.x, abs.y, rel.x, rel.y, updateFormat);
}



void UI_CoordinateWidget::setCoordinates(double x, double y,
        double rx, double ry, bool updateFormat) {
    
    if (graphic!=NULL) {
        if (updateFormat) {
            format = graphic->getLinearFormat();
            prec = graphic->getLinearPrecision();
            aformat = graphic->getAngleFormat();
            aprec = graphic->getAnglePrecision();
        }
    
        // abs / rel coordinates:
        PF_String absX = PF_Units::formatLinear(x,
                                               graphic->getUnit(),
                                               format, prec);
        PF_String absY = PF_Units::formatLinear(y,
                                               graphic->getUnit(),
                                               format, prec);
        PF_String relX = PF_Units::formatLinear(rx,
                                               graphic->getUnit(),
                                               format, prec);
        PF_String relY = PF_Units::formatLinear(ry,
                                               graphic->getUnit(),
                                               format, prec);
        
        lCoord1->setText(absX + " , " + absY);
        lCoord2->setText(relX + " , " + relY);        
        
        // polar coordinates:
        PF_Vector v;
        v = PF_Vector(x, y);
        QString str;
        QString rStr = PF_Units::formatLinear(v.magnitude(),
                                               graphic->getUnit(),
                                               format, prec);
        QString aStr = PF_Units::formatAngle(v.angle(),
                                               aformat, aprec);
        
        str = rStr + " < " + aStr;
        lCoord1b->setText(str);
    
        v = PF_Vector(rx, ry);
        rStr = PF_Units::formatLinear(v.magnitude(),
                                               graphic->getUnit(),
                                               format, prec);
        aStr = PF_Units::formatAngle(v.angle(),
                                               aformat, aprec);
        str = rStr + " < " + aStr;
        lCoord2b->setText(str);
    }
}
